package com.kakavr.split.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.beust.jcommander.internal.Maps;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.shop.service.ITradeFlowService;
import com.kakavr.split.service.ISplitLogService;
import com.kakavr.util.DateTimeUtils;
import com.kakavr.util.SessionUtils;
import com.kakavr.util.ValidatorUtils;

@RestController
@RequestMapping("split")
public class SplitLogController extends BaseController {
	@Resource
	private ISplitLogService splitLogService;
	@Resource
	private ITradeFlowService tradeFlowService;

	/**
	 * 分账记录
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectList", method = RequestMethod.GET)
	public Map<String, Object> selectSplitList(HttpServletRequest request) {
		String storeId = request.getParameter("storeId");
		String name = request.getParameter("storeName");
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("status", request.getParameter("status"));
		map.put("partnerId", SessionUtils.getPartnerId());
		if (StringUtils.isNotBlank(storeId)) {
			map.put("storeId", storeId);
		}
		if (StringUtils.isNotBlank(name)) {
			map.put("name", name);
		}
		return MsgResult
				.okResultData(splitLogService.selectSplitLogByPage(map));
	}

	/**
	 * 分账详情
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectInfo", method = RequestMethod.GET)
	public Map<String, Object> selectSplitInfo(HttpServletRequest request) {
		String splitId = request.getParameter("splitId");
		if (!ValidatorUtils.isNumber(splitId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "分账id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("splitId", splitId);
		map.put("partnerId", SessionUtils.getPartnerId());
		return MsgResult.okResultData(splitLogService.selectSplitInfo(map));
	}

	/**
	 * 交易流水记录
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectTradeFlowList", method = RequestMethod.GET)
	public Map<String, Object> selectTradeFlowList(HttpServletRequest request) {
		String storeId = request.getParameter("storeId");
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("begin", request.getParameter("begin"));
		map.put("end", request.getParameter("end"));
		map.put("splitId", request.getParameter("splitId"));
		map.put("type", request.getParameter("type"));//区别是活动店铺还是 普通分账
		if (StringUtils.isNotBlank(storeId)) {
			map.put("storeId", storeId);
		}
		map.put("partnerId", SessionUtils.getPartnerId());
		return MsgResult
				.okResultData(tradeFlowService.selectTradeFlowList(map));
	}

	/**
	 * 店铺分析
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "statisticsTradeFlow", method = RequestMethod.GET)
	public Map<String, Object> statisticsTradeFlow(HttpServletRequest request) {
		String type = request.getParameter("type");
		String begin = request.getParameter("begin");
		String end = request.getParameter("end");
		Map<String, Object> map = Maps.newHashMap();
		if (StringUtils.isNotBlank(begin)) {
			map.put("begin", begin);
		}
		if (StringUtils.isNotBlank(end)) {
			map.put("end", end);
		}
		Date time = DateTime.now().toDate();
		map.put("type", type);
		map.put("partnerId", SessionUtils.getPartnerId());
		if ("week".equals(type)) {
			map.put("begin", DateTimeUtils.getThisWeekMonday(time));
		}
		map.put("time", time);
		return MsgResult
				.okResultData(tradeFlowService.statisticsTradeFlow(map));
	}

	/**
	 * 分账
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateSplit", method = RequestMethod.POST)
	public Map<String, Object> updateSplitInfo(HttpServletRequest request) {
		String splitId = request.getParameter("splitId");
		if (!ValidatorUtils.isNumber(splitId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "分账id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", splitId);
		splitLogService.updateSplitLog(map);
		return MsgResult.okResult();
	}

	/**
	 * 统计分账
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "statisSplit", method = RequestMethod.GET)
	public Map<String, Object> statisticsSplit(HttpServletRequest request) {
		String storeId = request.getParameter("storeId");
		String status = request.getParameter("status");
		Map<String, Object> map = Maps.newHashMap();
		if (StringUtils.isNotBlank(storeId)) {
			map.put("storeId", storeId);
		}
		if (StringUtils.isNotBlank(status)) {
			map.put("status", status);
		}
		map.put("partnerId", SessionUtils.getPartnerId());
		return MsgResult.okResultData(splitLogService.statisticsSplit(map));
	}
	
	/**
	 * 审核凭证
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "examVoucher", method = RequestMethod.POST)
	public Map<String, Object> examVoucherSplit(HttpServletRequest request){
		String splitId = request.getParameter("splitId");
		if (!ValidatorUtils.isNumber(splitId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "分账id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", splitId);
		map.put("imgStatus", request.getParameter("imgStatus"));
		splitLogService.updateSplitLogByExam(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 活动收入账目列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "activitySplit", method = RequestMethod.POST)
	public Map<String, Object> activitySplit(HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		return MsgResult.okResultData(tradeFlowService.statisActivityTradeFlowList(map));
	}
	
	/**
	 * 活动收入账目  确认收款更新
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateactivitySplit", method = RequestMethod.POST)
	public Map<String, Object> updateactivitySplit(HttpServletRequest request){
		String storeId=request.getParameter("storeId");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("storeId", storeId);
		tradeFlowService.updateTradeFlow(map);
		return MsgResult.okResult();
	}
	

}
